<template>
  <div class="category">
    <div class="btn-row">
      <el-button type="primary" @click="showAddDlg = true">新增机构类别</el-button>
    </div>
    <el-table :data="list" style="width: 100%; height: 100%">
      <el-table-column label="序号" width="180" prop="pos"></el-table-column>
      <el-table-column label="名称" width="180" prop="name"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleShowDetail(scope.$index, scope.row)">查看机构</el-button>
          <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)">编辑机构</el-button>
          <el-button
            size="mini"
            :disabled="scope.$index === 0"
            type="primary"
            @click="handleUpward(scope.$index, scope.row)"
          >位置上移</el-button>
          <el-button
            size="mini"
            :disabled="scope.$index === list.length - 1"
            type="success"
            @click="handleDownward(scope.$index, scope.row)"
          >位置下移</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="deleteOrgCategory(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <show-org-dlg :showDlg="showViewDlg" :categoryId="selectedId" @close="handleCloseShowDetail"></show-org-dlg>
    <edit-org-dlg :showDlg="showEditDlg" :categoryId="selectedId" @close="handleCloseEditDlg"></edit-org-dlg>
    <add-org-dlg
      :showDlg="showAddDlg"
      @cancle="showAddDlg = false"
      @confirm="handleAddOrgCategory"
    />
  </div>
</template>
<script>
import { Button, Table, TableColumn } from "element-ui";
import {
  getFirstLoanOrgCategoryList,
  deleteOrgCategory,
  upwardOrgCategory,
  downwardOrgCategory
} from "@/api/first-loan.js";
import ShowOrgDlg from "./components/ShowOrgDlg";
import EditOrgDlg from "./components/EditOrgDlg";
import AddOrgDlg from "./components/AddOrgDlg";
export default {
  name: "first-loan-org-category",
  components: {
    [Button.name]: Button,
    [Table.name]: Table,
    [TableColumn.name]: TableColumn,
    ShowOrgDlg,
    EditOrgDlg,
    AddOrgDlg
  },
  data() {
    return {
      list: [],
      selectedId: 0,
      showViewDlg: false,
      showEditDlg: false,
      showAddDlg: false
    };
  },
  methods: {
    refreshList() {
      getFirstLoanOrgCategoryList()
        .then(res => {
          this.list = res;
        })
        .catch(errMsg => this.$alert(errMsg.msg));
    },
    handleShowDetail(index, row) {
      this.selectedId = this.list[index].id;
      this.showViewDlg = true;
    },
    handleCloseShowDetail() {
      this.showViewDlg = false;
    },
    handleEdit(index, row) {
      this.selectedId = this.list[index].id;
      this.showEditDlg = true;
    },
    handleCloseEditDlg() {
      this.showEditDlg = false;
    },
    deleteOrgCategory(index) {
      this.$confirm("确认删除该类别？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteOrgCategory(this.list[index].id)
            .then(res => this.refreshList())
            .catch(errMsg => this.$alert(errMsg.msg));
        })
        .catch(() => {});
    },
    handleAddOrgCategory() {
      this.showAddDlg = false;
      this.refreshList();
    },
    handleUpward(index) {
      upwardOrgCategory(this.list[index].id)
        .then(res => this.refreshList())
        .catch(errMsg => this.$alert(errMsg.msg));
    },
    handleDownward(index){
      downwardOrgCategory(this.list[index].id)
        .then(res => this.refreshList())
        .catch(errMsg => this.$alert(errMsg.msg));
    }
  },
  mounted() {
    this.refreshList();
  }
};
</script>
<style lang="stylus" scoped></style>